﻿/*
 * My Timer - cdTimerControl.cs
 * 
 * Copyright (C) 2009 Ahmad Hanis Azmi
 * 
 * This file is part of My Timer
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Windows.Forms;

namespace My_Timer
{
    public class TimerControl
    {
        public static void StartTimer(int i_Tag)
        {
            if (GlobalClass.r_TimerControl.ContainsKey(i_Tag) == false)
            {
                //ok, i messed up here, this is quick dirty hack.. hopefully this works!
                //see timerControl_Tick event in frmTray
                Timer t_TimerControl = new Timer();
                t_TimerControl.Tick += new EventHandler(Program.r_frmTray.timerControl_Tick);
                GlobalClass.r_TimerControl.Add(i_Tag, t_TimerControl);
            }

            TimersArray t_TimersArray;
            t_TimersArray = GlobalClass.r_Timers[i_Tag];

            if ((GlobalClass.r_frmMain.Created == true) || (GlobalClass.r_frmMain.Visible == true))
            {
                t_TimersArray.iIndexOfKey = GlobalClass.r_frmMain.lvTimers.Items.IndexOfKey(i_Tag.ToString());
                int i_IndexOfKey = t_TimersArray.iIndexOfKey;
                GlobalClass.r_frmMain.lvTimers.Items[i_IndexOfKey].Text = t_TimersArray.Name + " (Running)";
            }

            if (t_TimersArray.IsFormEnable == true)
            {
                t_TimersArray.TimerForm.pictureBox1.Image = global::My_Timer.Properties.Resources.media_playback_pause_16x16;
            }

            t_TimersArray.timerState = 1;

            GlobalClass.r_Timers[i_Tag] = t_TimersArray;

            GlobalClass.r_TimerControl[i_Tag].Tag = i_Tag;
            GlobalClass.r_TimerControl[i_Tag].Interval = 100;
            GlobalClass.r_TimerControl[i_Tag].Enabled = true;
        }

        public static void PauseTimer(int i_Tag)
        {
            if (GlobalClass.r_TimerControl.ContainsKey(i_Tag) == true)
            {
                TimersArray t_TimersArray;
                t_TimersArray = GlobalClass.r_Timers[i_Tag];

                t_TimersArray.timerState = 2;

                GlobalClass.r_TimerControl[i_Tag].Enabled = false;

                if ((GlobalClass.r_frmMain.Created == true) || (GlobalClass.r_frmMain.Visible == true))
                {
                    t_TimersArray.iIndexOfKey = GlobalClass.r_frmMain.lvTimers.Items.IndexOfKey(i_Tag.ToString());
                    int i_IndexOfKey = t_TimersArray.iIndexOfKey;
                    GlobalClass.r_frmMain.lvTimers.Items[i_IndexOfKey].Text = t_TimersArray.Name + " (Paused)";
                }

                if (t_TimersArray.IsFormEnable == true)
                {
                    t_TimersArray.TimerForm.pictureBox1.Image = global::My_Timer.Properties.Resources.media_playback_start_16x16;
                }

                GlobalClass.r_Timers[i_Tag] = t_TimersArray;
            }
        }

        public static void StopTimer(int i_Tag)
        {
            if (GlobalClass.r_TimerControl.ContainsKey(i_Tag) == true)
            {
                GlobalClass.r_TimerControl[i_Tag].Enabled = false;

                TimersArray t_TimersArray = GlobalClass.r_Timers[i_Tag];

                t_TimersArray.tDays = t_TimersArray.Days;
                t_TimersArray.tHours = t_TimersArray.Hours;
                t_TimersArray.tMinutes = t_TimersArray.Minutes;
                t_TimersArray.tSeconds = t_TimersArray.Seconds;
                t_TimersArray.t_1_over_10_Seconds = 0;
                t_TimersArray.timerState = 0;

                string s = "";

                s = Formatting.Formatting.formatDaysHoursMinutesSecondsLong(t_TimersArray.tDays,
                    t_TimersArray.tHours, t_TimersArray.tMinutes, t_TimersArray.tSeconds, 0);

                if ((GlobalClass.r_frmMain.Created == true) || (GlobalClass.r_frmMain.Visible == true))
                {
                    t_TimersArray.iIndexOfKey = GlobalClass.r_frmMain.lvTimers.Items.IndexOfKey(i_Tag.ToString());
                    int i_IndexOfKey = t_TimersArray.iIndexOfKey;
                    GlobalClass.r_frmMain.lvTimers.Items[i_IndexOfKey].Text = t_TimersArray.Name;
                    GlobalClass.r_frmMain.lvTimers.Items[i_IndexOfKey].SubItems[1].Text = s;
                }

                if (t_TimersArray.IsFormEnable == true)
                {
                    t_TimersArray.TimerForm.pictureBox1.Image = global::My_Timer.Properties.Resources.media_playback_start_16x16;
                    s = Formatting.Formatting.formatDaysHoursMinutesSecondsShort(t_TimersArray.tDays,
                        t_TimersArray.tHours, t_TimersArray.tMinutes, t_TimersArray.tSeconds, 0);

                    t_TimersArray.TimerForm.labelTimer.Text = s;
                }

                GlobalClass.r_Timers[i_Tag] = t_TimersArray;
            }
        }
    }
}